// 封装请求
import axios, {AxiosInstance} from "axios";
import {message} from 'ant-design-vue'
// 请求
const http: AxiosInstance = axios.create({
    // baseURL: process.env.VUE_APP_API_URL||'',
    timeout: 6000,
});
// 请求拦截
http.interceptors.request.use(
    (config: any) => {
        //请求头设置
        const token = localStorage.getItem("token") || "";
        config.headers.Authorization = token;
        config.headers["Content-Type"] = "application/json;charset=UTF-8";
        return config;
    },
    (err: any) => {
        console.log(err);
    }
);
// 响应拦截
http.interceptors.response.use(
    (res: any) => {
        const {data} = res as apiResult<any>
        if (data.state === 200) {
            return data;
        } else {
            // 这里已经是接口异常了，不需要回调了
            message.error(data?.message)
            throw new Error(data?.message)
        }
    },
    (err: any) => {
        console.log(err);
    }
);

export const request = async <T=any>({method = "get", url = "", data = {}, params = {},}) =>
    await http({
        method,
        url,
        data,
        params,
    }) as unknown as apiResult<T>

export type apiResult<T=any> = {
    cause: "";
    data: T;
    message: string;
    request: null;
    state: number;
    total: number;
    variables: any;
    version: null;
};
